package com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.listener.ad_action

import android.content.Context
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.ATNetworkConfirmInfo
import com.anythink.core.api.AdError
import com.anythink.splashad.api.ATSplashAdExtraInfo
import com.anythink.splashad.api.ATSplashExListener
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsCount
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.TopOnInterAdManager
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.TopOnOpenAdManager
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import java.util.Timer
import java.util.TimerTask

class ATSplashExListenerImpl : ATSplashExListener {

    private var adListener: TopOnOpenAdListener? = null
    private var onRefresh = true

    fun setListener(topOnOpenAdListener: TopOnOpenAdListener?) {
        this.adListener = topOnOpenAdListener
    }

    //加载完成
    override fun onAdLoaded(isTimeout: Boolean) {
        adListener?.onAdLoaded()
        onRefresh = true
        LogUtil.iTopOn("TopOn 【开屏广告】 onAdLoaded isTimeout:$isTimeout")
    }

    //加载超时后，直接进入主界面
    override fun onAdLoadTimeout() {
        adListener?.onAdLoadTimeout()
        LogUtil.iTopOn("TopOn 【开屏广告】 onAdLoadTimeout ")
    }

    //加载失败直接进入主界面
    override fun onNoAdError(adError: AdError) {
        if (onRefresh) {
            onRefresh = false
            //延迟10秒加载
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    TopOnInterAdManager.manualLoadAd()
                    LogUtil.iTopOn("TopOn 开屏加载失败 延迟10s后重新请求 【开屏广告】。。。。。。。。")
                }
            }, 10000)
        }
        adListener?.onNoAdError()
        LogUtil.iTopOn(
            "TopOn 【开屏广告】 onNoAdError :" + adError.fullErrorInfo
        )
    }

    override fun onAdShow(entity: ATAdInfo) {
        AdsCount.incrementDisplayCount()
        BpApplication.adIsShow = true
        adListener?.onAdShow()
        LogUtil.iTopOn("TopOn 【开屏广告】 onAdShow 广告来源：${entity.networkName}")
    }

    override fun onAdClick(entity: ATAdInfo) {
        AdsCount.incrementClickCount()
        adListener?.onAdClick()
        LogUtil.iTopOn("TopOn 【开屏广告】 onAdClick :$entity")
    }


    override fun onAdDismiss(entity: ATAdInfo, splashAdExtraInfo: ATSplashAdExtraInfo) {
        BpApplication.adIsShow = false
        adListener?.onAdDismiss()
        LogUtil.iTopOn("TopOn 【开屏广告】 onAdDismiss :$entity")
//        TopOnOpenAdManager.loadAd()
    }

    //deeplink回调，针对Adx、OnlineApi广告 isSuccess：是否成功
    override fun onDeeplinkCallback(entity: ATAdInfo, isSuccess: Boolean) {
        adListener?.onDeeplinkCallback(entity, isSuccess)
        LogUtil.iTopOn(
            "TopOn 【开屏广告】 TopOn 【开屏广告】 onDeeplinkCallback ：$entity isSuccess = $isSuccess"
        )
    }

    //应用类广告点击下载时回调方法
    override fun onDownloadConfirm(
        context: Context, adInfo: ATAdInfo, networkConfirmInfo: ATNetworkConfirmInfo
    ) {
        adListener?.onDownloadConfirm(context, adInfo, networkConfirmInfo)
        LogUtil.iTopOn(
            "TopOn 【开屏广告】 TopOn 【开屏广告】 onDownloadConfirm  entity = $adInfo"
        )
    }
}
